package edu.sandau.app.basic.service.impl;

import edu.sandau.app.basic.domain.OrderDetail;
import edu.sandau.app.basic.domain.OrderDetailExample;
import edu.sandau.app.basic.mapper.OrderDetailMapper;
import edu.sandau.app.basic.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    
    @Override
    public List<OrderDetail> getOrderDetailsByOrderId(Long orderId) {
        OrderDetailExample example = new OrderDetailExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        return orderDetailMapper.selectByExample(example);
    }
    
    @Override
    public OrderDetail getOrderDetailById(Long id) {
        return orderDetailMapper.selectByPrimaryKey(id);
    }
    
    @Override
    @Transactional
    public int addOrderDetail(OrderDetail orderDetail) {
        orderDetail.setCreateTime(new Date());
        return orderDetailMapper.insertSelective(orderDetail);
    }
    
    @Override
    @Transactional
    public int batchAddOrderDetails(List<OrderDetail> orderDetails) {
        int count = 0;
        for (OrderDetail orderDetail : orderDetails) {
            orderDetail.setCreateTime(new Date());
            count += orderDetailMapper.insertSelective(orderDetail);
        }
        return count;
    }
    
    @Override
    @Transactional
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
    }
    
    @Override
    @Transactional
    public int deleteOrderDetail(Long id) {
        return orderDetailMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public List<OrderDetail> getOrderDetailsByDishId(Long dishId) {
        OrderDetailExample example = new OrderDetailExample();
        example.createCriteria().andDishIdEqualTo(dishId);
        return orderDetailMapper.selectByExample(example);
    }
}

